from django.db import models

# Create your models here.
from django.db import models
from User.models import MyUser


class Commodity(models.Model):
    # 商品名（char）: 金冠玫瑰
    name = models.CharField(max_length=100, verbose_name='商品名')

    # 商品描述(char)
    description = models.TextField(verbose_name='商品描述')

    # 商品类别（一级）(char)：家电
    category_first = models.CharField(max_length=50, verbose_name='商品类别（一级）', blank=True)

    # 商品类别（二级）(char)：电视/音响
    category_second = models.CharField(max_length=50, verbose_name='商品类别（二级）', blank=True)

    # 商品颜色（varchar）：一款玫瑰色
    color = models.CharField(max_length=100, verbose_name='商品颜色', blank=True)

    # 商品参数（varchar）：11, 22, 33
    parameters = models.CharField(max_length=100, verbose_name='商品参数', blank=True)

    # 商品原价 (double)：3000
    original_price = models.FloatField(verbose_name='商品原价')

    # 商品现价 (double)：2599
    current_price = models.FloatField(verbose_name='商品现价')

    # 商品成交量 (int)：可用于推荐算法
    turnover = models.IntegerField(verbose_name='商品成交量', default=0)

    # 商品库存（卖家用）(int)：例：88
    stock = models.IntegerField(verbose_name='商品库存（卖家用）', default=0)

    # 商品图片 (image)：（每件商品三张图片可以放成三张表，一张为静态）
    img1 = models.ImageField(upload_to='goods', verbose_name='商品图片1')

    img2 = models.ImageField(upload_to='goods', verbose_name='商品图片2')

    img3 = models.ImageField(upload_to='goods', verbose_name='商品图片3')
    store = models.ForeignKey('Store.Store', on_delete=models.CASCADE, verbose_name='店铺', blank=True, null=True)

    class Meta:
        verbose_name = '商品'
        verbose_name_plural = '商品'

    def __str__(self):
        return self.name


class Order(models.Model):
    # 订单id，Django会自动创建一个id字段作为主键，所以这里不需要再显式声明

    related_commodity = models.ManyToManyField(
        Commodity,
        related_name="included_commodity",
        verbose_name="商品",
        blank=True,
        through='OrderShip'
    )
    address = models.ForeignKey(
        'Address',
        on_delete=models.CASCADE,
        verbose_name='地址',
    )

    # 创建时间
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    # 买家订单状态

    status_buyer = models.CharField(
        max_length=50,
        verbose_name='买家订单状态')

    # 卖家订单状态
    status_merchant = models.CharField(
        max_length=50,
        verbose_name='卖家订单状态')

    # 用户ID
    # 这里假设有一个User模型和Order模型相关联
    user = models.ForeignKey(
        'User.MyUser',
        on_delete=models.CASCADE,
        verbose_name='用户ID',
        related_name='user_orders'
    )
    # 商家ID
    # 这里假设有一个Merchant模型和Order模型相关联
    merchant = models.ForeignKey(
        'User.MyUser',
        on_delete=models.CASCADE,
        verbose_name='商家ID',
        related_name='merchant_orders'
    )

    class Meta:
        verbose_name = '订单'
        verbose_name_plural = '订单'
    def __str__(self):
        return str(self.id)


class OrderShip(models.Model):
    order = models.ForeignKey(
        Order,
        on_delete=models.CASCADE,
        verbose_name='订单ID'
    )
    commodity = models.ForeignKey(
        Commodity,
        on_delete=models.CASCADE,
        verbose_name='商品ID'
    )
    quantity = models.IntegerField(verbose_name='购买数量')

    subtotal = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        verbose_name='小计'
    )


class Address(models.Model):
    user = models.ForeignKey(
        'User.MyUser',
        on_delete=models.CASCADE,
        verbose_name='用户ID'
    )
    nickname = models.CharField(max_length=100, verbose_name='收货人姓名')
    phone = models.CharField(max_length=100, verbose_name='收货人电话')
    address = models.CharField(max_length=100, verbose_name='收货地址')


    def __str__(self):
        return self.address


class Cart(models.Model):
    user = models.ForeignKey(
        'User.MyUser',
        on_delete=models.CASCADE,
        verbose_name='用户ID'
    )
    related_commodity = models.ManyToManyField(
        Commodity,
        related_name="cart_commodity",
        verbose_name="商品",
        blank=True,
        through='CartShip',
    )


class CartShip(models.Model):
    cart = models.ForeignKey(
        Cart,
        on_delete=models.CASCADE,
        verbose_name='购物车'
    )
    commodity = models.ForeignKey(
        Commodity,
        on_delete=models.CASCADE,
        verbose_name='商品'
    )
    quantity = models.IntegerField(verbose_name='购买数量')


class Favorite(models.Model):
    user = models.ForeignKey(
        'User.MyUser',
        on_delete=models.CASCADE,
        verbose_name='用户ID'
    )
    commodity = models.ManyToManyField(
        Commodity,
        verbose_name="商品",
        blank=True,
    )
